<template>
  <div>
    <div class="search">
      <el-form :inline="true" :model="form" class="demo-form-inline">
        <el-row>
          <el-col :span="16">
            <el-form-item>
              <el-select clearable v-model="form.status" placeholder="请选择我们的状态">
                <el-option label="未审核" value="未审核"></el-option>
                <el-option label="审核通过" value="审核通过"></el-option>
                <el-option label="审核不通过" value="审核不通过"></el-option>
                <el-option label="推荐" value="推荐"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-select clearable v-model="form.categoryId" placeholder="请选择栏目分类">
                <el-option v-for="item in categories" :key="item.id" :label="item.name" :value="item.id"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-input clearable v-model="form.title" placeholder="请输入文章标题"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item>
              <el-button @click="toPublish" type="primary">发布文章</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

    </div>
    <!-- 表格开始 -->
    <div class="table">
      <el-table :data="articleData" style="width: 100%">
        <el-table-column type="index" :index="1" label="序号" width="180">
        </el-table-column>
        <el-table-column label="封面" width="220">
          <template v-slot="scope">
            <div>
              <img height="80px" width="150px" :src="scope.row.cover" alt="">
            </div>
          </template>
        </el-table-column>
        <el-table-column label="风采" width="650">
          <template v-slot="scope">
            <div>
              <div>{{ scope.row.title }}</div>
              <div>{{ scope.row.baseUser ? scope.row.baseUser.realname : '佚名' }}</div>
              <div>{{ scope.row.category.name }}</div>
              <div>{{ scope.row.publishTime | fmtDate }}</div>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="status" label="状态" align="center">
          <template v-slot="scope">
            <div>
              <el-tag v-if="scope.row.status === '审核通过'" type='success'>审核通过</el-tag>
              <el-tag v-else-if="scope.row.status === '未审核'" type='info'>未审核</el-tag>
              <el-tag v-else-if="scope.row.status === '审核不通过'" type='warning'>审核不通过</el-tag>
              <el-tag v-else type='danger'>推荐</el-tag>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="address" label="操作" align="center">
          <template v-slot="scope">
            <div>
              <el-link @click="toRecommoned(scope.row.id)" :underline="false" type="warning">推荐</el-link>
              <el-link @click="toCheck(scope.row)" :disabled="scope.row.status !== '未审核'" :underline="false"
                style="margin-left: 10px; " type="primary">审核</el-link><br>
              <el-link :underline="false" type="danger">删除</el-link>
              <el-link @click="toEdit(scope.row)" :underline="false" style="margin-left: 10px; " type='success'>修改</el-link>
            </div>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <div class="page" style="margin-top:15px">
      <el-pagination hide-on-single-page background @size-change="handleSizeChange" @current-change="handleCurrentChange"
        :current-page="params.page" :page-sizes="[5, 10, 15, 20]" :page-size="params.pageSize"
        layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </div>
    <!-- 审核模态框 -->
    <el-dialog title="审核文章" :visible.sync="visible" width="50%">
      <div style="text-align: center;">{{ rowArticle.title }}</div>
      <div v-html="rowArticle.content"></div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="changeStatus(rowArticle.id, '审核不通过')" type="danger">审核不通过</el-button>
        <el-button @click="changeStatus(rowArticle.id, '审核通过')" type="primary">审核通过</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { get } from '@/utils/request'
import { post } from '@/utils/request'
export default {
  data() {
    return {
      // 根据条件查询表单对象
      form: {},
      // 保存所有栏目
      categories: [],
      // 表格数据源
      articleData: [],
      // 分页对象
      params: {
        page: 1,
        pageSize: 5
      },
      // 数据总条数
      total: 0,
      // 点击审核接收当前被点击文章审核对象
      rowArticle: {},
      // 控制模态框显示与隐藏变量
      visible: false
    }
  },
  methods: {
    // 修改文章
    toEdit(row){
      this.$router.push({
        path:'/consult/publish',
        query:row
      })
    },
    // 发布文章
    toPublish() {
      this.$router.push({
        path: '/consult/publish'
      })
    },
    // 审核文章
    async changeStatus(id, status) {
      let res = await post('/article/check', { id, status })
      if (res.status === 200) {
        this.$notify({
          title: 'res.message',
          message: '审核成功',
          type: 'success'
        });
        this.visible = false
        this.getArticle()
      } else {
        this.$notify.error({
          title: '失败',
          message: '审核失败',
          type: 'success'
        })
      }
    },
    // 审核文章
    toCheck(row) {
      console.log(row);
      this.rowArticle = row
      this.visible = true;
    },
    toRecommoned(id) {
      this.$confirm('此操作将推荐该文章, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        let res = await post('/article/check', { id, status: "推荐" })
        if (res.status == 200) {
          this.$notify({
            title: 'res,message',
            message: '推荐成功',
            type: 'success'
          })
          this.getArticle()
        } else {
          this.$notify.error({
            title: '失败',
            message: '推荐失败',
            type: 'success'
          })
        }
      }).catch(() => {
        this.$notify({
          title: '取消推荐',
          type: 'info',
          message: '已取消推荐'
        });
      });
    },
    async getCategories() {
      let res = await get('/category/findAll')
      // console.log(res);
      this.categories = res.data
    },
    // 分页多条件获取文章信息
    async getArticle() {
      // 将分页对象属性和条件对象属性传给后端
      let temp = {
        ...this.params,//必传
        ...this.form
      }
      // 判断条件是否存在 存在传给后端  不存在删除条件
      for (let key in temp) {
        if (!temp[key]) {
          delete temp[key]
        }
      }
      let res = await get('/article/pageQuery', temp)
      console.log(res);
      this.articleData = res.data.list;
      this.total = res.data.total
    },
    // 每页大小触发的方法  5条一页变成10条一夜
    handleSizeChange(pageSize) {
      // pageSize由5改为10
      this.params.page=1
      this.params.pageSize = pageSize;
      this.getArticle()
    },
    // 页面发生改变触发方法
    handleCurrentChange(page) {
      this.params.page = page;
      this.getArticle()
    },
  },
  // 监听form
  watch: {
    // form(){
    //   this.getArticle()
    // }
    form: {
      handler() {
        this.params.page = 1;
        this.getArticle()
      },
      deep: true
    }
  },
  created() {
    this.getCategories()
    this.getArticle()
  },
}
</script>